package Game;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;

public class PoliticAlly extends Ally
{
	public static enum PoliticAllyType
	{
		VEREADOR,
		DEPUTADO,
		SENADOR
	}
	
	PoliticAllyType politicType;
	
	float attackDelay;
	float maxAttackStrength;
	float minAttackStrength;
	float c;
	
	boolean isReturning = false;
	
	boolean move = false;
	
	public PoliticAlly(int x, int y, int w, int h, PoliticAllyType type, int id)
	{
		super(x, y, w, h, id);
		
		this.moving = LoadImage("/Images/animation_politic1_walking_back.png");
		
		this.politicType = type;
		
		minAttackStrength = 1f;
		
		this.type = AllyType.Politic;
		
		switch(type)
		{
			case VEREADOR:
				
				this.animation = new Animation(moving, 1, 4, 5, x, y, w, h);
				this.color = Color.DARK_GRAY;
				attackDelay = 1f;
				maxAttackStrength = minAttackStrength + r.nextFloat();
				lifeBar = new EnergyBar(new Point2D.Double(x, y), new Point2D.Double(10, 50), 10, true);
				
				break;
			case DEPUTADO:
				
				this.animation = new Animation(moving, 1, 4, 5, x, y, w, h);
				this.color = Color.orange;
				attackDelay = 2f;
				maxAttackStrength = minAttackStrength + r.nextFloat() * 5;
				lifeBar = new EnergyBar(new Point2D.Double(x, y), new Point2D.Double(20, 50), 10, true);
				
				break;
			case SENADOR:
				
				this.animation = new Animation(moving, 1, 4, 5, x, y, w, h);
				this.color = Color.green;
				attackDelay = 4f;
				maxAttackStrength = minAttackStrength + r.nextFloat() * 10;
				lifeBar = new EnergyBar(new Point2D.Double(x, y), new Point2D.Double(30, 50), 10, true);
				
				break;
		}
		
		c = attackDelay;
		
		float aux = 0;
		
		while ((int)aux == 0)
		{
			aux = r.nextFloat() * 3;
		}
		
		this.velocity = -aux;
		
		Move();
	}

	@Override
	public void Update() 
	{
		if(visible)
		{
			super.Update();
			
			if(lifeBar.CurrentEnergy <= 0)
			{
				Kill();
			}
			
			if(state != AllyState.DEAD)
			{
				if(Mouse.clicked)
				{
					if(getRectangle().contains(Mouse.lastClickedPosition))
					 {
						move = !move;
						
						if(AllyManager.CPIs != null)
						{
							for(CPI cpi : AllyManager.CPIs)
							{
								if(cpi.id == this.id)
								{
									cpi.spawnMove = move;
								}
							}
						}
						System.out.println("Aliado " + politicType.toString() + (move? " movendo": " parou"));
					 }
				}
				else if(Mouse.isPressed)
				{
					if(getRectangle().contains(Mouse.lastPressedPosition))
					 {
						if(Mouse.actualPosition.y < Mouse.lastPressedPosition.y) //Se o toque ocorreu de baixo pra cima, retornando = false
						{
							isReturning = false;
							move = true;
						}
						else if(Mouse.actualPosition.y > Mouse.lastPressedPosition.y) //Caso ocorra de cima pra baixo, retornando = true
						{
							isReturning = true;
							move = true;
						}
						else
						{
							move = !move;
						}
						
						if(AllyManager.CPIs != null)
						{
							for(CPI cpi : AllyManager.CPIs)
							{
								if(cpi.id == this.id)
								{
									cpi.spawnMove = move;
								}
							}
						}
						System.out.println("Aliado " + politicType.toString() + (move? " movendo": " parou"));
					 }
				}
			}
			
			switch(state)
			{
				case IDLE:
					
					CollisionUpdate();
					
					if(move)
						Move();
					
					break;
				case MOVING:
					
					if(isReturning)
					{
						y -= velocity;
					}
					else
					{
						y += velocity;
					}
					
					if(y + (h * 0.5f) < 0)
					{
						isReturning = true;
					}
					
					lifeBar.UpdatePosition(this.getRectangle());
					
					CollisionUpdate();
					
					if(!move)
						Stop();
					
					break;
				case ATTACKING:
					
					Attack();
					
					break;
				case DEAD:
					
					this.visible = false;
					
					break;
			}
		}	
	}
	
	private void CollisionUpdate()
	{
		for(Money m : MoneyManager.money)
		{
			if(m.getRectangle().intersects(getRectangle()))
			{
				isReturning = false;
			}
		}
		
		if(AllyManager.politics != null)
		{
			for(CPI ally : AllyManager.CPIs)
			{
				if(this.getRectangle().intersects(ally.getRectangle()))
				{
					if(ally.id == this.id)
					{
						if(isReturning)
						{
							//COLIDIU CPI RETORNANDO
							Stop();
							isReturning = false;
						}
						else
						{
							//Move();
						}
					}
					else
					{
						Kill();
					}
				}
			}
		}
		
		if(EnemyManager.inimigos != null)
		{
			for(Enemy enemy : EnemyManager.inimigos)
			{
				if(this.getRectangle().intersects(enemy.getRectangle()))
				{
					if(enemy.visible)
					{
						state = AllyState.ATTACKING;
					}
				}
			}
		}
	}
	
	@Override
	public void Attack() 
	{
		c+= 0.1f;
		
		if(c >= attackDelay)
		{
			c = 0;
			boolean attacked = false;
			
			if(EnemyManager.inimigos != null)
			{
				for(Enemy enemy : EnemyManager.inimigos)
				{
					if(this.getRectangle().intersects(enemy.getRectangle()))
					{
						if(enemy.visible)
						{
							int attackPower = (int)Math.max(minAttackStrength, r.nextInt((int)maxAttackStrength));
							enemy.lifeBar.CurrentEnergy -= attackPower;
							attacked = true;
						}
					}
				}
			}
			if(attacked == false)
			{
				if(move)
				{
					Move();
				}
				else
				{
					Stop();
				}
			}
		}
	}
	
	public void Draw(Graphics2D g2d)
	{
		if(visible)
		{
			super.Draw(g2d);
			
			Graphics2D previousGraphics = g2d;
			
			switch(state)
			{
				case ATTACKING:
					g2d.setColor(color);
					break;
				default:
					g2d.setColor(Color.darkGray);
					break;
			}
			
			g2d.drawString(politicType.toString(), x, y + h + 20);
			
			g2d = previousGraphics;
			
		}
	}
	
	public void Stop()
	{
		state = AllyState.IDLE;
		this.animationActive = false;
		move = false;
		visible = true;
	}
	
	public void Move()
	{
		this.animation = new Animation(moving, 1, 4, 5, x, y, w, h);
		state = AllyState.MOVING;
		this.animationActive = true;
		move = true;
		visible = true;
	}
	
	@Override
	public void Kill()
	{
		state = AllyState.DEAD;
		
		if(AllyManager.CPIs != null)
		{
			for(CPI cpi : AllyManager.CPIs)
			{
				if(cpi.id == this.id)
				{
					cpi.spawnDead = true;
				}
			}
		}
	}
}
